package com.huazai.apply.manager.web.action;

import com.huazai.apply.manager.dao.UserDao;
import com.huazai.apply.manager.dao.impl.UserDaoReal;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Apply", value = {"/add","/decide","/delete"})
public class ApplyServlet extends HttpServlet {
    private static final String SERVICE_ADD = "/add";
    private static final String SERVICE_DECIDE = "/decide";
    private static final String SERVICE_DELETE = "/delete";
    private static final String RESULT_PRE = "Prepared";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String servletPath = request.getServletPath();

        if ( servletPath.equals(SERVICE_ADD) ) {
            this.doAdd(request, response);
        } else if ( servletPath.equals(SERVICE_DECIDE) ) {
            this.doDecide(request, response);
        } else if ( servletPath.equals(SERVICE_DELETE) ) {
            this.doDelete(request, response);
        }
    }

    private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {

        UserDao userDao = new UserDaoReal();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();

        //0012-12-12T20:12,注意前端的传入数据是这样子的，所以需要以下的语句来进行修正
        String userid = request.getParameter("userid");
        String dateTime = request.getParameter("dateTime");
        String wantTime = request.getParameter("outTime");

        String replaceTime = dateTime.replace("T", " ");
        replaceTime += ":00";


        //replaceTime用于解决时间格式不匹配而出此下策。
        if (userDao.uploadSubmit(userid, replaceTime, Integer.parseInt(wantTime), RESULT_PRE) ) {
            pw.print("<h1 align=\"center\">申请提交成功</h1>");
        }
        else {
            pw.print("<h1 align=\"center\">申请提交失败</h1>");
        }
    }



    private void doDecide(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UserDao userDao = new UserDaoReal();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        String applyId = request.getParameter("applyID");
        String result = request.getParameter("result");

        if ( userDao.updateSubmit(applyId, result) ) {
            pw.print("<h1 align=\"center\">审批成功</h1>");
        }
        else {
            pw.print("<h1 align=\"center\">审批失败</h1>");
        }
        pw.print("<a href='javascript:void(0)' onclick='window.history.back()'>返回上一级</a>");
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UserDao userDao = new UserDaoReal();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();

        String applyId = request.getParameter("applyID");

        /**
         * 需要再去学习怎么把数据传到后端，继续跟老杜学一学
         * 解答：来在地址栏上解决
         */
        if ( userDao.deleteSubmit(applyId) ) {
            pw.print("<h1 align='center'>出校申请删除成功</h1>");
        }
        else {
            pw.print("<h1 align='center'>出校申请删除失败</h1>");
        }
        pw.print("<a href='javascript:void(0)' onclick='window.history.back()'>返回上一级</a>");
    }

}
